package org.jeecg.modules.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusRoomsLivingOrder;
import org.jeecg.modules.business.enums.SettleTypeEnum;
import org.jeecg.modules.business.mapper.BusBookingRoomsMapper;
import org.jeecg.modules.business.service.IBusBookingRoomsService;
import org.jeecg.modules.business.service.IBusRoomsLivingOrderService;
import org.jeecg.modules.business.vo.ExtendBusBookingRoomsVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 预定订单关联房间
 * @Author: jeecg-boot
 * @Date:   2023-03-25
 * @Version: V1.0
 */
@Service
public class BusBookingRoomsServiceImpl extends ServiceImpl<BusBookingRoomsMapper, BusBookingRooms> implements IBusBookingRoomsService {

    @Resource
    private IBusRoomsLivingOrderService livingOrderService;

    @Override
    public List<ExtendBusBookingRoomsVo> bookingRooms(String bookingId,Integer type) {
        return baseMapper.bookingRooms(bookingId,type);
    }

    @Override
    public Boolean setMain(String bookingRoomsId, String livingOrderId) {
        BusBookingRooms roomR =  getById(bookingRoomsId);
        if(roomR==null) throw new JeecgBootException("未找到房间");
        if (roomR.getIsMain()){
            throw new JeecgBootException("该房间已是主房");
        }
        if(livingOrderId == null || livingOrderId.isEmpty()){
            throw new JeecgBootException("未入住，订单不存在");
        }
        if (livingOrderId != null || !livingOrderId.isEmpty()){
            BusRoomsLivingOrder livingOrder = livingOrderService.getById(livingOrderId);
            if (livingOrder == null){
                throw new JeecgBootException("订单不存在");
            }
            if (!SettleTypeEnum.UN_LEAVE.getKey().equals(livingOrder.getSettleType())){
                throw new JeecgBootException("退房的房间不能设置为主房");
            }
        }
        baseMapper.setNotMain(roomR.getBookingOrdersId());
        roomR.setIsMain(true);
        updateById(roomR);
        return true;
    }

    @Override
    public List<BusBookingRooms> bookingRoomsDetail(List<String> booingIds) {
        if (CollUtil.isEmpty(booingIds)){
            return new ArrayList<>();
        }
        return baseMapper.bookingRoomsDetail(booingIds);
    }
}
